package com.life.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static Date getFutureDateExact(int day, int hours, int minutes, int seconds, boolean flag) {
		Date date0 = null;
		Date date1 = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date1);
		calendar.add(Calendar.DATE, day);
		calendar.set(Calendar.HOUR_OF_DAY, hours);
		calendar.set(Calendar.MINUTE, minutes);
		calendar.set(Calendar.SECOND, seconds);
		date0 = calendar.getTime();
		if (flag == true) {
			if (date0.before(date1)) {
				calendar.add(Calendar.DATE, 1);
				date0 = calendar.getTime();
			}
		}
		return date0;
	}

	public static Date getFutureDate(Date date, int year, int month, int day, int hours, int minutes, int seconds) {
		Date date0 = null;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, year);
		calendar.add(Calendar.MONTH, month);
		calendar.add(Calendar.DATE, day);
		calendar.add(Calendar.HOUR_OF_DAY, hours);
		calendar.add(Calendar.MINUTE, minutes);
		calendar.add(Calendar.SECOND, seconds);
		date0 = calendar.getTime();
		return date0;
	}

	public static Date getFutureDateExact(Date date, int day, int hours, int minutes, int seconds) {
		Date date0 = null;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, day);
		calendar.set(Calendar.HOUR_OF_DAY, hours);
		calendar.set(Calendar.MINUTE, minutes);
		calendar.set(Calendar.SECOND, seconds);
		date0 = calendar.getTime();
		return date0;
	}

	public static Date getFutureDateExact(Date date, int month, int day, int hours, int minutes, int seconds) {
		Date date0 = null;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, month);
		calendar.add(Calendar.DATE, day);
		calendar.set(Calendar.HOUR_OF_DAY, hours);
		calendar.set(Calendar.MINUTE, minutes);
		calendar.set(Calendar.SECOND, seconds);
		date0 = calendar.getTime();
		return date0;
	}

	public static String getFutureDateExact(Date date, int day, String regula) {
		Date date0 = null;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, day);
		date0 = calendar.getTime();
		return GetDateString(date0, regula);
	}

	public static String getFutureDateExact(Integer day, String regula) {
		Date date0 = null;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DATE, day);
		date0 = calendar.getTime();
		return GetDateString(date0, regula);
	}

	public static Date getFutureDateExact(Date date, int seconds) {
		Date date0 = null;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.SECOND, seconds);
		date0 = calendar.getTime();
		return date0;
	}

	public static void main(String[] args) {
		//System.out.println(DateUtil.ConvertToDate("Thu Nov 08 15:35:58 CST 2012", "yyyy-MM-dd"));
		//System.out.println(compareDates("2013-07-18 01:48:02", "2013-07-17 10:28:02", "yyyy-MM-dd hh:mm:ss"));
		//System.out.println(compareDate("2014-01-12", "2014-01-13"));
		
		String dateTime = "3014-8-27" +" 00:00:00";
		Date date = GetDate(dateTime, "yyyy-MM-dd HH:mm:ss");
		System.out.println(FormatDate(addHouseTime(date,17), "yyyy-MM-dd HH:mm:ss") );
		
	}

	public static Date GetDate(String time1, String regula) {
		SimpleDateFormat df2 = new SimpleDateFormat(regula);
		try {
			Date date1 = df2.parse(time1);
			return date1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Date();
	}

	public static Date GetLastDate(String time, String regula) {
		return GetLastDate(GetDate(time, regula));
	}

	public static Date GetLastDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}

	public static String GetLastDate(String time, String regula, String regula1) {
		return FormatDate(GetLastDate(time, regula), regula1);
	}

	public static String GetLastDateStr(Date date, String regula) {
		return FormatDate(GetLastDate(date), regula);
	}

	public static Date GetFirstDate(String time, String regula) {
		return GetFirstDate(GetDate(time, regula));
	}

	public static Date GetFirstDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	public static String GetFirstDate(String time, String regula, String regula1) {
		return FormatDate(GetFirstDate(time, regula), regula1);
	}

	public static String GetFirstDateStr(Date date, String regula) {
		return FormatDate(GetFirstDate(date), regula);
	}

	public static Date StrConvertToDate(String time1, String regula) {
		Date date = null;
		SimpleDateFormat df2 = new SimpleDateFormat(regula);
		try {
			date = df2.parse(time1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date ConvertToDate(Object obj, String regula) {
		SimpleDateFormat df2 = new SimpleDateFormat(regula);
		Date date = null;
		try {
			if (obj == null)
				date = new Date();
			else if (obj.getClass() == String.class)
				date = df2.parse((String) obj);
			else if (obj.getClass() == Timestamp.class)
				date = new Date(((Timestamp) obj).getTime());
			else if (obj.getClass() == java.sql.Date.class)
				date = new Date(((java.sql.Date) obj).getTime());
			else if (obj.getClass() == Date.class)
				date = (Date) obj;
			else
				date = new Date();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date ConvertToDate(Object obj) {
		Date date = null;
		try {
			if (obj == null)
				date = new Date();
			else if (obj.getClass() == Timestamp.class)
				date = new Date(((Timestamp) obj).getTime());
			else if (obj.getClass() == java.sql.Date.class)
				date = new Date(((java.sql.Date) obj).getTime());
			else if (obj.getClass() == Date.class)
				date = (Date) obj;
			else
				date = new Date();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String GetDateString(Object date, String regula) {
		if (date == null)
			return "";
		SimpleDateFormat df2 = new SimpleDateFormat(regula);
		return df2.format(date);
	}

	public static long getQuot(String time1, String time2) {
		if (StringUtil.ConvertToStr(time1).equals("") || StringUtil.ConvertToStr(time2).equals(""))
			return 0;
		long quot = 0;
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date1 = ft.parse(time1);
			Date date2 = ft.parse(time2);
			quot = date1.getTime() - date2.getTime();
			quot = quot / 1000 / 60 / 60 / 24;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
		return quot;
	}

	public static boolean compareDate(String time1, String time2) {
		try {
			SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
			Date date1 = ft.parse(time1);
			Date date2 = ft.parse(time2);
			if (date1.before(date2) || date1.equals(date2))
				return true;
			else
				return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static int compareDates(String time1, String time2, String regula) {
		int i = 0;
		try {
			SimpleDateFormat ft = new SimpleDateFormat(regula);
			Date date1 = ft.parse(time1);
			Date date2 = ft.parse(time2);
			if (date1.before(date2))
				i = -1;
			else if (date1.equals(date2))
				i = 0;
			else if (i == 1)
				i = 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}

	public static boolean IsInRange(String begin, String end, String time) {
		try {
			SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
			Date date1 = ft.parse(begin);
			Date date2 = ft.parse(end);
			Date date3;
			if (time.equals(""))
				date3 = ft.parse(getCurrentDate("yyyy-MM-dd"));
			else
				date3 = ft.parse(time);

			if ((date3.after(date1) && date3.before(date2)) || date3.equals(date1) || date3.equals(date2))
				return true;
			else
				return false;
		} catch (Exception e) {

		}
		return false;
	}

	public static boolean IsInRange(Date begin, Date end, Date time) {
		try {
			if ((time.after(begin) && time.before(end)) || time.compareTo(begin) == 0 || time.compareTo(end) == 0)
				return true;
			else
				return false;
		} catch (Exception e) {
		}
		return false;
	}

	public static String getCurrentDate(String regula) {
		Date date = new Date();
		SimpleDateFormat df2 = new SimpleDateFormat(regula);
		return df2.format(date);
	}

	public static String getFutureDate(int day, String regula) {
		// date.setDate(date.getDay() + day);
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, day);

		SimpleDateFormat df2 = new SimpleDateFormat(regula);
		return df2.format(calendar.getTime());
	}

	public static String getFutureDate(int day, String date, String regula) {
		SimpleDateFormat df2 = new SimpleDateFormat(regula);
		Calendar calendar = Calendar.getInstance();
		try {
			Date date1 = df2.parse(date);
			calendar.setTime(date1);
			calendar.add(Calendar.DATE, day);
			return df2.format(calendar.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return df2.format(calendar.getTime());
	}

	public static Date getFutureDate(int day) {
		Date date = null;
		Calendar calendar = Calendar.getInstance();
		try {
			Date date1 = new Date();
			calendar.setTime(date1);
			calendar.add(Calendar.DATE, day);
			date = calendar.getTime();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date getFutureDate(Date date, int day) {
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(date);
			calendar.add(Calendar.DATE, day);
			date = calendar.getTime();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String getFutureMonthDate(int day, String regula) {
		// date.setDate(date.getDay() + day);
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MARCH, day);
		SimpleDateFormat df2 = new SimpleDateFormat(regula);
		return df2.format(calendar.getTime());
	}

	public static String FormatDate(String date, String regula1, String regula2) {
		String result = "";
		if (StringUtil.ConvertToStr(date).equals(""))
			date = getCurrentDate(regula1);
		try {
			SimpleDateFormat df2 = new SimpleDateFormat(regula1);
			SimpleDateFormat df1 = new SimpleDateFormat(regula2);
			Date date1 = df2.parse(date);
			result = df1.format(date1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String FormatDate(Date date, String regula1) {
		String result = "";
		try {
			if (date != null) {
				SimpleDateFormat df1 = new SimpleDateFormat(regula1);
				result = df1.format(date);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 日期相加
	 * @param date
	 * @param number
	 * @return
	 */
	public static Date addDateTime(Date date,int number){
		Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.MONTH,number);
        return rightNow.getTime();
	}
	
	/**
	 * 日期相加(秒)
	 * @param date
	 * @param number
	 * @return
	 */
	public static Date addSecordTime(Date date,int number){
		Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.SECOND,number);
        return rightNow.getTime();
	}
	
	/**
	 * 日期相加(小时)
	 * @param date
	 * @param number
	 * @return
	 */
	public static Date addHouseTime(Date date,int number){
		Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.HOUR_OF_DAY,number);
        return rightNow.getTime();
	}
	
}
